<?php

namespace app\controller\api\module\game\components;

use app\components\traits\Singleton;
use app\controller\api\module\game\components\traits\Game;

class Match
{
    use Singleton, Game;
    protected string $modelName = 'match';

    /**
     * 记录棋局
     * @param int $x
     * @param int $y
     * @return array
     */
    public function record(int $x, int $y): array
    {
        if($x < 0 or $x > 14 or $y < 0 or $y > 14){
            return responseArrayMsg('数据异常了，请刷新页面重试');
        }
        $roomInfo = $this->checkRoomExist('id, uid, opposite_uid');
        $roomPlayerInfo = Model('roomPlayerInfo')->getInfo(['rid' => $roomInfo['id']], 'id, match_id, outcome', ['id' => 'desc']);
        if($roomPlayerInfo['outcome']> 0){
            return responseArrayMsg('棋局已经结束，请开始下一局');
        }
        $res = $this->model->add([
            'room_id' => getAPICaches('room_id_' . $this->baseInfo['id']),
            'match_id' => $roomPlayerInfo['match_id'],
            'uid' => $roomInfo['uid'],
            'opposite_uid' => $roomInfo['opposite_uid'],
            'x' => $x,
            'y' => $y,
            'chess' => $this->getRoomCache('player-' . $this->baseInfo['id'] . '.chess'),
            'addtime' => time(),
        ]);
        putLog('/match/record/' . date('Ym/d') . '.log', '记录落子操作|接收的参数：' . json_encode(request()->param()) . '，处理结果：' . $res . PHP_EOL);
        return responseArrayMsg('', 200);
    }

    /**
     * 悔棋
     * @return array
     */
    public function withdraw(): array
    {
        $this->checkRoomExist();
        $lastStepID = $this->model->getInfo(['room_id' => getAPICaches('room_id_' . $this->baseInfo['id'])], 'id', ['addtime' => 'desc']);
        if(empty($lastStepID)){
            return responseArrayMsg('没有可悔棋的棋子');
        }
        $editResult = $this->model->edit(['id' => $lastStepID], ['withdraw' => 1]);
        putLog('/match/withdraw/' . date('Ym/d') . '.log', '悔棋操作|接收的参数：' . json_encode(request()->param()) . '，处理结果：' . $editResult . PHP_EOL);
        return responseArrayMsg('', 200);
    }


    /**
     * 记录棋局结果
     * @return array
     */
    public function result(): array
    {
        $roomInfo = $this->checkRoomExist();
        $playerInfoID = Model('roomPlayerInfo')->getInfo(['rid' => $roomInfo['id']], 'id', ['id' => 'desc']);
        $editResult = Model('roomPlayerInfo')->edit(['id' => $playerInfoID], ['outcome' => values('outcome', 0), 'endtime' => time()]);
        putLog('/match/result/' . date('Ym/d') . '.log', '记录棋局结果2|接收的参数：' . json_encode(request()->param()) . '，处理结果：' . $editResult . PHP_EOL);
        return responseArrayMsg('', 200);
    }



    /**
     * 判断房间是否存在
     * @param string $filed         展示的字段
     * @return array|string
     */
    private function checkRoomExist(string $filed = 'id'){
        $roomID = getAPICaches('room_id_' . $this->baseInfo['id']) ? : '';
        empty($roomID) and responseMsgExit('房间号不存在');
        $roomInfo = Model('room')->getInfo(['room_id' => $roomID], !empty($filed) ? $filed . ', state, state_code' : 'id, state, state_code', ['addtime' => 'desc']);
        empty($roomInfo) and responseMsgExit('房间不存在');
        ($roomInfo['state'] != 1 or $roomInfo['state_code'] != 200) and responseMsgExit('房间已经关闭', 202);
        return $roomInfo;
    }
}